package cn.org.zx.common.commutils;

import java.io.IOException; 
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import cn.org.zx.db.dbconnection.DBProperties;

public class Upload extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
	@Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	
    	request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
           
		Map mapData = request.getParameterMap();
				
		String strcnumber = StringUtils.toString(mapData.get("selfGeneral"));
		System.out.println("strcnumber="+strcnumber);	
		String strFilePath = "";		
		//System.out.println("strcnumber="+strcnumber);				
		String allowExt = "jpg,png,jpeg,bmp,JPG,PNG,JPEG,BMP";
		String strfilesavepath = DBProperties.getPathFile();
		
		Map mapReturn = new HashMap();
		
		if(!strcnumber.equals("")){
			strcnumber = strcnumber.toUpperCase();
			boolean b = FileManage.getCreateDirectory(strfilesavepath,strcnumber);
			String strfilename = "";
			if(b){				
				com.zx.jspsmart.upload.UploadService us = new com.zx.jspsmart.upload.UploadService(allowExt,strfilesavepath+strcnumber);
				us.setTotalMaxFileSize(5242880);		//5M以下
				if(us.upload(request, response, this.getServletContext())){
					strfilename = us.getFiles().getFile(0).getSaveName();
					strFilePath = "file/cpatrain/"+strcnumber+"/"+strfilename;
					mapReturn.put("url", strFilePath);
					mapReturn.put("code", "0");
				}
				else{
					mapReturn.put("code", "500");
				}
			}
		}
		else{
			mapReturn.put("code", "500");
		}
				
		PrintWriter out = null;
		try {
			out = response.getWriter();
			String result = Json.toJson(mapReturn);
			out.print(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
	 } 
}
